<template>
  <el-card class="login-card">
    <!--<div class="div-icon">
      <el-avatar icon="el-icon-user-solid"></el-avatar>
    </div>-->
    <div class="div-icon">
      <el-avatar icon="el-icon-user-solid" :size="60"> </el-avatar>
    </div>
    <el-form
      ref="loginFormRef"
      :rules="rules"
      label-position="left"
      :model="loginForm"
      label-width="52px"
    >
      <el-form-item label="账号" prop="username">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="loginForm.username"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          v-model="loginForm.password"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item prop="rem">
        <el-checkbox label="记住密码" v-model="loginForm.rem"></el-checkbox>
      </el-form-item>
      <el-button-group class="login-btn">
        <el-button type="primary" @click="btnLogin" :loading="isLoading"
          >登录</el-button
        >
        <el-button type="primary" @click="btnReset">重置</el-button>
      </el-button-group>
    </el-form>
  </el-card>
</template>

<script>
import { api_login } from '../api/admin.js'

export default {
  data() {
    return {
      isLoading: false,
      loginForm: {
        username: '',
        password: '',
        rem: false
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 4, max: 11, message: '长度在4到11个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在6到11个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    btnReset() {
      //1.获取表单对象
      // console.log(this.$refs.loginFormRef);
      //2.通过表单对象执行
      this.$refs['loginFormRef'].resetFields()
    },
    btnLogin() {
      this.isLoading = true
      // 1.验证表单
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          console.log('验证失败')
          return false
        }
        // 2.提交衣单
        const { data: result } = await api_login(
          this.loginForm.username,
          this.loginForm.password
        )
        this.isLoading = false
        //console.log(result)
        if (result.code !== 200) {
          this.$message.error(result.message)
          return true
        }

        const data = result.data
        const token = data.tokenHead + data.token
        /* api_login(this.loginForm.username,this.loginForm.password).then(({data}) => {
            //正常请求得到响应，账号密码出错也在这
            console.log(data)
          }).catch(err => {
            //请求出错，找不到服务器，服务bug
            console.log(err)
          })*/
        // 3.保存token到sessionStorage
        window.sessionStorage.setItem('token', token) //会话结束后，自动消除
        // 4.保存用户信息到vuex中的store
        // 5.记住密码功能，登录将用户密码保存到cookies
        if (this.loginForm.rem) {
          this.$cookies.set('username', this.loginForm.username, '7D') //默认是一天过期
          this.$cookies.set('password', this.loginForm.password, '7D')
        } else {
          this.$cookies.remove('username')
          this.$cookies.remove('password')
        }
        //6.登录页面刷新或重新加载的时候从cookies中获取，在created()函数中
        //7.跳转到主页面，编程式路由跳转
        await this.$router.replace('/home')
      })
    }
  },
  created() {
    //获取cookies中的值
    this.loginForm.username = this.$cookies.get('username')
    this.loginForm.password = this.$cookies.get('password')
  }
}
</script>

<style scoped>
/**scoped表示该组件中的样式只是在改组件有效**/
.div-icon {
  transform: translateX(50%);
  margin-bottom: 30px;
}
.login-card {
  width: 500px;
  margin: auto;
  transform: translateY(50%);
}
.login-btn {
  width: 100%;
}
.login-btn .el-button {
  width: 50%;
}
</style>
